<script>
import { options, pushTask } from "@/api/robot/robotTaskAdPushUnit";
import { globalLoading, isNull } from "@/utils/service";

export default {
  name: "pushTaskDialog",
  data() {
    return {
      show: false,
      lookMe: true,
      form: {
        isTest: false,
        pushUnitIds: [],
        robotNumberList: [],
      },
      options: {
        pushUnit: {
          data: [],
          loading: true,
          loadingText: "加载中...."
        }
      }
    };
  },
  methods: {
    open(robotNumberList) {
      if (isNull(robotNumberList)){
        this.$message.warning("尚未选择机器")
        return
      }
      this.form.robotNumberList = robotNumberList;
      this.form.isTest = false;
      this.form.pushUnitIds = []
      this.show = true;
    },
    getOptionsPushUnit() {
      this.options.pushUnit.loading = true;
      options(this.lookMe).then(r => {
        this.options.pushUnit.data = r;
      }).finally(() => {
        this.options.pushUnit.loading = false;
      });
    },
    pushTaskReq(){
      let loading = globalLoading(this);
      pushTask(this.form).then(r => {
        this.show = false;
        this.$message.success("发布成功")
        this.$emit("refreshTb")
      }).finally(() => {
        loading.close();
      })
    }
  }
};
</script>

<template>
  <div>
    <el-dialog
      title="发布任务"
      :visible.sync="show"
      width="35%"
    >
      <el-form :model="form" size="mini" label-suffix=" : ">
        <el-form-item label="执行环境" prop="isTest">
          <el-radio-group v-model="form.isTest">
            <el-radio :label=false>线上</el-radio>
            <el-radio :label=true>测试</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="发布任务单元" prop="pushUnitIds">
          <el-select v-model="form.pushUnitIds" placeholder="请选择任务发布单元" value-key="id"
                     @focus="getOptionsPushUnit" style="width: 60%;margin-right: 5px" multiple
          >
            <el-option
              v-for="item in options.pushUnit.data"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
              <span style="float: left">{{ item.name }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">用户编号 : {{ item.adId }}</span>
            </el-option>
          </el-select>
          <el-radio-group v-model="lookMe">
            <el-radio-button :label=false>所有</el-radio-button>
            <el-radio-button :label=true>自己</el-radio-button>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template slot="footer">
        <el-button type="info" size="small" @click="show=false">取消</el-button>
        <el-button type="primary" size="small" @click="pushTaskReq">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>

</style>